package demo1;

public class Test {
    private int a;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static int find(int[] arr, int index){
        if(arr == null){
            throw new NullPointerException("空指针异常");
        }
        if(index > arr.length){
            throw new ArrayIndexOutOfBoundsException("数组越界访问异常");
        }
        return arr[index];
    }

    public static void main1(String[] args) {
        try{
            int[] arr = {1,2,3};
            System.out.println(arr[3]);
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        System.out.println("后序代码");
    }

    public static void main2(String[] args) {
        int[] arr = {1,2,3};

        try{
            System.out.println(arr[5]);
        }catch(NullPointerException e){
            System.out.println("空指针异常");
            e.printStackTrace();
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("数组越界访问异常");
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("Exception 异常发生");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try{
            int[] arr = {1,2,3};
            arr[5] = 10;
            arr[0] = 20;
        }catch (NullPointerException e){
            e.printStackTrace();
        }finally {
            System.out.println("finally 的代码执行了");
        }

        System.out.println("如果没有抛出异常，或者异常被处理了,try-catch 后的代码也执行了");
    }

}
